package permutations;

import java.util.ArrayList;
import java.util.List;

public class Permutations {
    static List<List<Integer>> result_temp = new ArrayList<>();
    public static List<String> getPermutations(Integer[] nums) {
        result_temp.clear();
        List<String> result = new ArrayList<>();
        // 递归求全排列
        recursion(nums, new ArrayList<>());
        for (int i = 0; i < result_temp.size(); i++) {
            String list_str = result_temp.get(i).toString();
            result.add(list_str);
        }
        return result;
    }

    static List<List<Integer>> recursion(Integer[] nums, List<Integer> list) {
        List<Integer> temp = new ArrayList<>(list);
        if (temp.size() == nums.length) {
            result_temp.add(temp);
        }
        for (int i = 0; i < nums.length; i++) {
            if (temp.contains(nums[i])) {
                continue;
            }
            temp.add(nums[i]);
            recursion(nums, temp);
            temp.remove(temp.size() - 1);
        }

        return result_temp;
    }
}
